/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.service.Impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import web.Dao.BookDAO;
import web.Dao.OrderDAO;
import web.Dao.impl.BookDAOImpl;
import web.Dao.impl.OrderDAOImpl;
import web.enity.Book;
import web.enity.Cart;
import web.enity.CartItem;
import web.enity.Item;
import web.enity.Order;
import web.enity.OrderDital;
import web.service.BookService;
import web.service.OrderService;

/**
 *
 * @author 10592
 */
public class OrderServiceImpl implements OrderService{

   
    @Override
    public boolean placeOrder(Cart cart) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        boolean result=false;
        if (cart!=null) {
            
            OrderDAO odao = new OrderDAOImpl();
            BookService bookService = new BookServiceImpl();
            //初始化订单
            Order order = new Order(Integer.MAX_VALUE, cart.getUserInfo().getUserName(), new Date(System.currentTimeMillis()));
            //插入订单数据
            odao.insertOrder(order);
            System.out.println("oid=" + order.getOid());
            for (CartItem cartItem : cart.getItems()) {
                //查询商品库存
                int checkStock = bookService.checkStock(cartItem.getBook().getBid(), cartItem.getNumber());
                if (checkStock >= 0) {
                    //更新商品库存
                    bookService.decreaseStock(cartItem.getBook().getBid(), cartItem.getNumber());
                    //初始化item对象
                    Item item = new Item(Integer.MIN_VALUE, order.getOid(), cartItem.getBook().getBid(), cartItem.getNumber(), String.valueOf(cartItem.getPrice()));
                    System.err.println(item.toString());
                    //写入数据库
                    odao.insertItems(item);
                }
            }
        }
       
 
        return true;
    }

    @Override
    public List<OrderDital> quaryItem(String userName) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<OrderDital> ods=null;
        OrderDAO odao=new OrderDAOImpl();
        ods=odao.quaryOrderiItems(userName);
        for (OrderDital od : ods) {
            System.err.println(od.toString());
        }
        return ods;
    }
    public static void main(String[] args) {
        OrderService OrderService = new OrderServiceImpl();
        OrderService.quaryItem("222");
    }
}
